/*!
 * Table Page Template v0.0.1
 * Author: manpiaoyi
 * Email: manpiaoyi@126.com
 * Date: 2014-11-1
 * Description: common js
 */

define(function(require, exports, module){
	
	var $ = require('jquery');
	require('jquery/jquery-nicescroll/3.5.4/jquery.nicescroll.js');
	require('jbox');
	var cookie=require("../util/cookie.js")
	//alert(typeof cookie.setCookie);
	
	//reset menu list
	for(var i=0,len=$("#sidebar .sub-menu").length;i<len;i++){
		if(cookie.getCookie("menu-list"+i)==0){
			$("#sidebar .sub-menu").eq(i).addClass("open").find("ul").slideUp(200);
		}
	}

//  sidebar dropdown menu
    $('#sidebar .sub-menu > a').click(function (e) {
        var last = jQuery('.sub-menu.open', $('#sidebar'));
        var sub = jQuery(this).next();   
        if (sub.is(":visible")) {
        	cookie.setCookie("menu-list"+jQuery(this).parent().index(),0,1);
            jQuery('.arrow', jQuery(this)).removeClass("open");
            jQuery(this).parent().removeClass("open");            
            sub.slideUp(200);
        } else {
        	$("#sidebar .sub-menu > a").not(this).parent().removeClass("open");
        	$("#sidebar .sub-menu > a").not(this).next().slideUp(200);
        	$(".arrow").removeClass("open");
        	//.removeClass("open");
        	var menu = $("#sidebar .sub-menu > a").not(this);
        	for(var i=0;i<menu.length;i++)
        		{
        	cookie.setCookie("menu-list"+i,0,1);    
        		}
        	cookie.setCookie("menu-list"+jQuery(this).parent().index(),1,1);   
            jQuery('.arrow', jQuery(this)).addClass("open");
            jQuery(this).parent().addClass("open");            
            sub.slideDown(200);
        }
        // var o = ($(this).offset());
        // var diff = 200 - o.top;
        // if(diff>0)
        //     $(".sidebar-scroll").scrollTop("-="+Math.abs(diff),500);
        // else
        //     $(".sidebar-scroll").scrollTop("+="+Math.abs(diff),500);
    });
//    sidebar toggle

    $('#toggle-menu').click(function () {
        if ($('#sidebar > ul').is(":visible") === true) {
            $('#main-content').css({
                'margin-left': '0px'
            });
            $('#sidebar').css({
                'margin-left': '-180px'
            });
            $('#sidebar > ul').hide();
            $("#container").addClass("sidebar-closed");
        } else {
            $('#main-content').css({
                'margin-left': '180px'
            });
            $('#sidebar > ul').show();
            $('#sidebar').css({
                'margin-left': '0'
            });
            $("#container").removeClass("sidebar-closed");
        }
    });

// custom scrollbar
    $(".sidebar-scroll").niceScroll({styler:"fb",cursorcolor:"#4A8BC2", cursorwidth: '5', cursorborderradius: '0px', background: '#404040', cursorborder: ''});
    $(".portlet-scroll-1").niceScroll({styler:"fb",cursorcolor:"#4A8BC2", cursorwidth: '5', cursorborderradius: '0px', background: '#404040', cursorborder: ''});
    $(".portlet-scroll-2").niceScroll({styler:"fb",cursorcolor:"#4A8BC2", cursorwidth: '5', cursorborderradius: '0px', autohidemode: false, cursorborder: ''});
    $(".portlet-scroll-3").niceScroll({styler:"fb",cursorcolor:"#4A8BC2", cursorwidth: '5', cursorborderradius: '0px', background: '#404040', autohidemode: false, cursorborder: ''});
    $("html").niceScroll({styler:"fb",cursorcolor:"#4A8BC2", cursorwidth: '8', cursorborderradius: '0px', background: '#404040', cursorborder: '', zindex: '1000'});

	
	$("input[type='text']").on("click", function () {
	   $(this).select();
	});
	
	// 引入js和css文件
	exports.include = function(id, path, file){
		if (document.getElementById(id)==null){
	        var files = typeof file == "string" ? [file] : file;
	        for (var i = 0; i < files.length; i++){
	            var name = files[i].replace(/^\s|\s$/g, "");
	            var att = name.split('.');
	            var ext = att[att.length - 1].toLowerCase();
	            var isCSS = ext == "css";
	            var tag = isCSS ? "link" : "script";
	            var attr = isCSS ? " type='text/css' rel='stylesheet' " : " type='text/javascript' ";
	            var link = (isCSS ? "href" : "src") + "='" + path + name + "'";
	            document.write("<" + tag + (i==0?" id="+id:"") + attr + link + "></" + tag + ">");
	        }
		}
	};

	// 打开一个窗体
	exports.windowOpen = function(url, name, width, height){
		var top=parseInt((window.screen.height-height)/2,10),left=parseInt((window.screen.width-width)/2,10),
			options="location=no,menubar=no,toolbar=no,dependent=yes,minimizable=no,modal=yes,alwaysRaised=yes,"+
			"resizable=yes,scrollbars=yes,"+"width="+width+",height="+height+",top="+top+",left="+left;
		window.open(url ,name , options);
	};

	// 显示加载框
	exports.loading = function(mess){
		top.$.jBox.tip.mess = null;
		top.$.jBox.tip(mess,'loading',{opacity:0});
	};

	// 确认对话框
	exports.confirmx = function(mess, href){
		top.$.jBox.confirm(mess,'系统提示',function(v,h,f){
			if(v=='ok'){
				exports.loading('正在提交，请稍等...');
				location = href;
			}
		},{buttonsFocus:1});
		top.$('.jbox-body .jbox-icon').css('top','55px');
		return false;
	};
	
	// 获得当前日期
	exports.getCurrentDate = function getCurrentDate(){
		var today = new Date();
		var y = today.getFullYear(); // 获取完整的年份(4位,1970-????)
		var m = today.getMonth() + 1; // 获取当前月份(0-11,0代表1月)
		var d = today.getDate(); // 获取当前日(1-31)
		var t_today = ''; // 获取当前时间
		if (m < 10) {
			m = 0 + "" + m;
			t_today = y + "" + m + "" + d;
		} else {
			t_today = y + "" + m + "" + d;
		}
		return t_today;
	};
	
	//---------------------------------------------------  
	// 日期格式化  
	// 格式 YYYY/yyyy/YY/yy 表示年份  
	// MM/M 月份  
	// W/w 星期  
	// dd/DD/d/D 日期  
	// hh/HH/h/H 时间  
	// mm/m 分钟  
	// ss/SS/s/S 秒  
	//---------------------------------------------------  
	exports.Format =  function(date,format){ 
		var o = { 
				"M+" : date.getMonth()+1, //month 
				"d+" : date.getDate(), //day 
				"h+" : date.getHours(), //hour 
				"m+" : date.getMinutes(), //minute 
				"s+" : date.getSeconds(), //second 
				"q+" : Math.floor((date.getMonth()+3)/3), //quarter 
				"S" : date.getMilliseconds() //millisecond 
				} ;

				if(/(y+)/.test(format)) { 
				format = format.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length)); 
				} 

				for(var k in o) { 
				if(new RegExp("("+ k +")").test(format)) { 
				format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); 
				} 
				} 
				return format; 
				} ;
});
